package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<Activity> getActivityList(ActivityDto activityDto) {
       //1.设置分页参数,调用pageHelper中的page.Starter方法
        PageHelper.startPage( activityDto.getPage(),activityDto.getPageSize());
        //2.调用mapper查询
        List< Activity> getActivityList = activityMapper.selectList(activityDto);
        //3.强转类型为page,调用page方法
        Page<Activity> p = (Page<Activity>) getActivityList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void deleteActivity(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public void addActivity(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    @Override
    public Activity getActivityById(Integer id) {
       return activityMapper.selectById(id);
    }

    @Override
    public void updateActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> getAll() {
        return activityMapper.getAll();
    }

    @Override
    public List<Activity> getActivityByType(Integer type) {
        return activityMapper.getActivityByType(type);
    }
}
